
layui.use(['form', 'layer', 'table', 'formSelects'], function(){

    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        table = layui.table;
    var formSelects = layui.formSelects;
    var show_url = $('.show_url').val();
    var edit_url = $('.edit_url').val();
    var InterValObj; //timer变量，控制时间
    var count = 60; //间隔函数，1秒执行
    var curCount;//当前剩余秒数

    // Form 表单
    function formHtml(data){
        console.log(data)
        if(data == 1){
            var content = show_url;
        }else {
           var content = edit_url;
        }
        // 右侧面板
        var index = layer.open({
            type: 2
            , anim: 1
            , title: false
            , closeBtn: false
            , offset: ''
            , shade: 0.1
            //, shadeClose: true // 是否点击遮罩关闭
            , skin: 'layui-anim layui-anim-rl layui-layer-adminRight'
            , area: ['600px','280px']
            , top: 0
            , content: content
        });
    }

    // 自定义验证规则
    form.verify({
        phone: function(value){
            if(value == ''){
                return '请输入手机号';
            }
            if(!/^1(3|4|5|6|7|8|9)[0-9]\d{8}$/.test(value)){   //^[0-9a-zA-Z]*[a-zA-Z][0-9a-zA-Z]*$
                return '手机号格式不正确';
            }
        },
        secrect:function (value) {
            if(value == ''){
                return '请输入secrect';
            }
            if(!/^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@#$%^&*`~()-+=]+$)(?![0-9\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\W_!@#$%^&*`~()-+=]{20}$/.test(value)){
                return 'secrect格式不正确';
            }
        }
    });

    // 提交显示token
    form.on("submit(doshowSecrect)", function(data){
        // 弹出loading
        if(data.field.code == ''){
            layer.msg("请填写验证码");
            return false;
        }
        $.ajax({
            url: "/api/Sms/checkApiSmsCode",
            data: data.field,
            type: "post",
            dataType: "json",
            success: function(res){
                if(res.status == 1){
                    $("#show_token").text(res.data);
                }else {
                    layer.msg(res.message);
                    $("#show_token").text('');
                }
            },
            error:function(data){
                layer.msg("服务器无响应");
            }
        });
    })

    form.on("submit(doupdateSecrect)", function(data){
        // 弹出loading
        if(data.field.code == ''){
            layer.msg("请填写验证码");
            return false;
        }
        $.ajax({
            url: "/api/Sms/checkApiSmsCode",
            data: data.field,
            type: "post",
            dataType: "json",
            success: function(res){
                if(res.status == 1){
                    $.ajax({
                        url: "/admin/Setapi/doeditsecrect",
                        data: data.field,
                        type: "post",
                        dataType: "json",
                        success: function(res){
                            if(res.code == 1){
                                layer.msg("修改成功");
                                layer.closeAll("iframe");
                            }else {
                                layer.msg(res.msg);
                            }
                        },
                        error:function(data){
                            layer.msg("服务器无响应");
                        }
                    });
                }else {
                    layer.msg(res.message);
                    $("#show_token").text('');
                }
            },
            error:function(data){
                layer.msg("服务器无响应");
            }
        });
    })

    form.on("submit(showSecrect)", function(data){
        formHtml(1);
    })

    form.on("submit(updateSecrect)", function(data){
        formHtml(2);
    })

    form.on("submit(SendSms)", function(data){
        $.ajax({
            url: '/api/Sms/ApisendSms',
            data: data.field,
            type: "post",
            dataType: "json",
            success: function(res){
                if(res.status == 1){
                    layer.msg('短信发送成功');
                    curCount = count;
                    //设置button效果，开始计时
                    $("#send_btn").css("background-color","#d0d0d0")
                    $("#send_btn").attr("disabled", "true");
                    $("#send_btn").val(curCount + "秒后可重新发送");
                    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次
                }else {
                    layer.msg(res.message);
                }
            },
            error:function(data){
                layer.msg("服务器无响应");
            }
        });
    })

    function SetRemainTime() {
        if (curCount == 0) {
            window.clearInterval(InterValObj);//停止计时器
            $('#send_btn').css('background-color', '#1E9FFF');
            $("#send_btn").removeAttr("disabled");//启用按钮
            $("#send_btn").text("重新发送验证码");
        }
        else {
            curCount--;
            $("#send_btn").text(curCount + "秒后可重新发送");
        }
    }

});